import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { getProductSimpleList } from '#/api/erp/product/product';
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'productId',
      label: $t('views.erp.common.product'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.productPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getProductSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'warehouseId',
      label: $t('views.erp.common.warehouse'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.warehousePlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'bizType',
      label: $t('views.erp.stock.record.bizType'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.erp.stock.record.bizTypePlaceholder'),
        allowClear: true,
        options: getDictOptions(DICT_TYPE.ERP_STOCK_RECORD_BIZ_TYPE, 'number'),
      },
    },
    {
      fieldName: 'bizNo',
      label: $t('views.erp.stock.record.bizNo'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.stock.record.bizNoPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'productName',
      title: $t('views.erp.common.product'),
      minWidth: 150,
    },
    {
      field: 'categoryName',
      title: $t('views.erp.product.product.category'),
      width: 120,
    },
    {
      field: 'unitName',
      title: $t('views.erp.product.product.unit'),
      width: 100,
    },
    {
      field: 'warehouseName',
      title: $t('views.erp.common.warehouse'),
      width: 120,
    },
    {
      field: 'bizType',
      title: $t('views.erp.stock.record.bizType'),
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_STOCK_RECORD_BIZ_TYPE },
      },
    },
    {
      field: 'bizNo',
      title: $t('views.erp.stock.record.bizNo'),
      width: 200,
      showOverflow: 'tooltip',
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'count',
      title: $t('views.erp.stock.record.count'),
      width: 120,
      formatter: 'formatAmount3',
    },
    {
      field: 'totalCount',
      title: $t('views.erp.stock.record.totalCount'),
      width: 100,
      formatter: 'formatAmount3',
    },
    {
      field: 'creatorName',
      title: $t('views.erp.stock.record.creator'),
      width: 100,
    },
  ];
}